package com.freda.oa.flow.support.impl;

import java.util.List;

import org.eredlab.g4.bmf.base.BaseServiceImpl;
import org.eredlab.g4.ccl.datastructure.Dto;

import com.freda.oa.flow.run.model.impl.FlowBaseModelImpl;
import com.freda.oa.flow.run.vo.FlowCommonListenerVo;
import com.freda.oa.flow.run.vo.FlowRunInfoVo;
import com.freda.oa.flow.support.Itf.FlowListenerSupportItf;

/**
 * 流程监控
 * @author Rally
 *
 */
public class FlowListenerSupportImpl extends FlowBaseModelImpl implements FlowListenerSupportItf {
	
	/* (non-Javadoc)
	 * @see com.freda.oa.flow.support.impl.FlowListenerSupportItf#CreateAllListeners(com.freda.oa.flow.run.vo.FlowRunInfoVo, java.lang.String)
	 */
	@Override
	public FlowCommonListenerVo CreateAllListeners(FlowRunInfoVo runInfo,String eventName)
	{
		List listenerList = super.g4Dao.queryForList("FlowDeployHandle.queryEvent",runInfo);
		
		FlowCommonListenerVo lisVo = (FlowCommonListenerVo) super.getBean("listenerVo");
		
		for(int i=0;listenerList != null && i < listenerList.size();i++)
		{
			if(((Dto)listenerList.get(i)).get("eventname") != null && ((Dto)listenerList.get(i)).get("eventname").toString().equals(""))
			{
				if(((Dto)listenerList.get(i)).get("eventname").toString().toLowerCase().equals("before"))
				{
					lisVo.setBeforeStr(((Dto)listenerList.get(i)).get("eventcode").toString());
				}
				else if(((Dto)listenerList.get(i)).get("eventname").toString().toLowerCase().equals("completed"))
				{
					lisVo.setCompletedStr(((Dto)listenerList.get(i)).get("eventcode").toString());
				}
				else if(((Dto)listenerList.get(i)).get("eventname").toString().toLowerCase().equals("open"))
				{
					lisVo.setOpenStr(((Dto)listenerList.get(i)).get("eventcode").toString());
				}
			}
		}
		
		lisVo.createAllListeners(eventName);
		
		return lisVo;
	}

	@Override
	public int excuteListeners(FlowRunInfoVo runInfo,FlowCommonListenerVo listenerVo, String eventName) {
		
		if(eventName != null && eventName.equals("before"))
		{
			if(listenerVo.isHasBLObject())
			{
				for(int i=0;i<listenerVo.getBLObjectSize();i++)
				{
					if(listenerVo.getListBeforeListeners().get(i).excute(runInfo) != 1)
					{
						System.out.println("");
						return 0;
					}
				}
			}
		}
		else if(eventName != null && eventName.equals("completed"))
		{
			if(listenerVo.isHasCLObject())
			{
				for(int i=0;i<listenerVo.getCLObjectSize();i++)
				{
					if(listenerVo.getListCompletedListeners().get(i).excute(runInfo) != 1)
					{
						System.out.println();
						return 0;
					}
				}
			}
		}
		else if(eventName != null && eventName.equals("open"))
		{
			if(listenerVo.isHasOLObject())
			{
				for(int i=0;i<listenerVo.getOLObjectSize();i++)
				{
					if(listenerVo.getListOpenListeners().get(i).excute(runInfo) != 1)
					{
						System.out.println();
						return 0;
					}
				}
			}
		}
		
		return 1;
	}

}
